package cn.lili.security;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Sylow
 * @Description 路由式过滤器 - 根据url前缀分发请求的认证过滤器
 * @Date: Created in 15:39 2025/6/11
 */
@Slf4j
public class AuthenticationRouterFilter extends OncePerRequestFilter {

    private final ManagerAuthenticationFilter managerAuthenticationFilter;
    private final BuyerAuthenticationFilter buyerAuthenticationFilter;
    private final StoreAuthenticationFilter storeAuthenticationFilter;



    public AuthenticationRouterFilter(ManagerAuthenticationFilter managerAuthenticationFilter, BuyerAuthenticationFilter buyerAuthenticationFilter, StoreAuthenticationFilter storeAuthenticationFilter) {
        this.managerAuthenticationFilter = managerAuthenticationFilter;
        this.buyerAuthenticationFilter = buyerAuthenticationFilter;
        this.storeAuthenticationFilter = storeAuthenticationFilter;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        String path = httpServletRequest.getRequestURI();
        if (path.startsWith("/manager")) {
            // 管理员请求
            managerAuthenticationFilter.doFilterInternal(httpServletRequest, httpServletResponse, filterChain);
        } else if (path.startsWith("/buyer")) {
            // 买家请求
            buyerAuthenticationFilter.doFilterInternal(httpServletRequest, httpServletResponse, filterChain);
        } else if (path.startsWith("/store")) {
            // 卖家请求
            storeAuthenticationFilter.doFilterInternal(httpServletRequest, httpServletResponse, filterChain);
        } else {
            // 其他请求，直接放行
            filterChain.doFilter(httpServletRequest, httpServletResponse);
        }
    }
}
